package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.pojo.dto.BrandDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    //根据品牌id集合查询品牌集合
    @Override
    public List<BrandDTO> queryBrandsById(List<Long> ids) {
        return BrandDTO.convertEntityList(this.listByIds(ids));
    }

    //根据分类id查询品牌
    @Override
    public List<BrandDTO> queryBrandsByCategoryId(Long id) {
        //先查询分类id对应的品牌id再查询品牌信息
        return BrandDTO.convertEntityList(baseMapper.queryBrandsByCategoryId(id));
    }

    //分页查询品牌
    @Override
    public PageDTO<BrandDTO> queryBrandsPage(Integer page, Integer rows, String key) {
        Page<Brand> brandPage = new Page<>(page, rows);
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        brandQueryWrapper.like(key != null, "name", key).or().eq(key != null, "letter", key);
        Page<Brand> page1 = this.page(brandPage, brandQueryWrapper);
        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(page1.getRecords());
        return new PageDTO<>(page1.getTotal(), page1.getSize(), brandDTOS);
    }

    //根据id查询品牌
    @Override
    public BrandDTO queryBrandById(String id) {
        return new BrandDTO(this.getById(id));
    }

    //新增品牌
    @Override
    @Transactional
    public void addBrand(BrandDTO brandDTO, List<String> categoryIds) {
        Brand brand = brandDTO.toEntity(Brand.class);
        //先存储品牌获取品牌id
        if (this.save(brand)) {
            saveBrandAndCategory(categoryIds, brand.getId());
        }
    }


    //更新品牌
    @Override
    @Transactional
    public void updateBrand(List<String> categoryIds, BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        //先将分类和品牌中间表中品牌对应的数据删除再添加
        removeBrandAndCategoryByBrandId(brand.getId());
        this.saveBrandAndCategory(categoryIds,brand.getId());
        //更新品牌表数据
        this.updateById(brand);
    }



    //根据id删除品牌
    @Override
    @Transactional
    public void deleteBrandById(Long id) {
        //先清除中间表中该品牌对应的信息
        this.removeBrandAndCategoryByBrandId(id);
        //再删除品牌表中的数据
        this.removeById(id);
    }

    //添加中间表数据
    private void saveBrandAndCategory(List<String> categoryIds, Long id) {
        for (String categoryId : categoryIds) {
            baseMapper.addBrandAndCategory(categoryId, id);
        }
    }

    //删除中间表中拼配对应的数据
    private void removeBrandAndCategoryByBrandId(Long id) {
        baseMapper.delBrandAndCategoryByBrandId(id);
    }
}
